package ru.sazonov.ide.model.java

import ru.sazonov.ide.model.ModelPart

class JavaArtefactAdapter {
    def ModelPart modelPart;
    
    def Klass findKlass(String klass) {
        klasses().find {it.fullName == klass};
    }
    
    def Method findMethod(String klass, String method) {
        findKlass(klass).methods.find {it.name == method};
    }
    
    def Property findProperty(String klass, String property) {
        findKlass(klass).properties.find {it.name == property};
    }
    
    def boolean hasKlass(String klass) {
        findKlass(klass) != null;
    }
    
    def boolean klassHasMethod(String klass, String method) {
        findMethod(klass, method) != null;
    }
    
    def boolean klassHasProperty(String klass, String property) {
        findProperty(klass, property) != null;
    }
    
    def boolean klassHasWritableProperty(String klass, String property) {
        def p = findProperty(klass, property)
        p.write;
    }
    
    def boolean klassHasReadableProperty(String klass, String property) {
        def p = findProperty(klass, property)
        p.read;
    }
    
    def boolean klassExtends(String klass, String doesExtends) {
        findKlass(klass)?.doesExtends?.find {it == doesExtends} != null;
        false;
    }
    
    def boolean klassImplements(String klass, String doesImplements) {
        findKlass(klass)?.doesImplements?.find {it == doesImplements} != null;
        false;
    }
    
    def List<Klass> klassesThatExtends(String klass) {
        klasses().findAll {it.doesExtends.contains(klass)};
    }
    
    def List<Klass> klassesThatImplements(String klass) {
        klasses().findAll {it.doesImplements.contains(klass)};
    }
    
    def List<Klass> klasses() {
        new ArrayList<Klass>((Collection<Klass>)modelPart.artefacts.findAll {
            it instanceof Klass;
        });
    }
}                                            
